<template>
  <jy-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="92px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="所属科室" prop="dpmId">
            <el-select v-model="form.dpmId" clearable placeholder="请选择所属科室" style="width: 100%">
              <el-option v-for="item in bsDataReportList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="仪器名称" prop="instruName">
            <el-input v-model="form.instruName" placeholder="请输入仪器名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="仪器编号" prop="instruCode">
            <el-input v-model="form.instruCode" placeholder="请输入仪器编号" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="生产厂家名称" prop="manufacturer">
            <el-input v-model="form.manufacturer" placeholder="请输入生产厂家名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="型号规格" prop="model">
            <el-input v-model="form.model" placeholder="请输入型号规格" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="测量范围" prop="measuringRange">
            <el-input v-model="form.measuringRange" placeholder="请输入测量范围" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出厂编号" prop="manufacturerCode">
            <el-input v-model="form.manufacturerCode" placeholder="请输入出厂编号" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="出厂日期" prop="manufacturerDate">
            <el-date-picker clearable v-model="form.manufacturerDate" type="date" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择出厂日期" style="width: 100%"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="购置日期" prop="purchaseDate">
            <el-date-picker clearable v-model="form.purchaseDate" type="date" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择购置日期" style="width: 100%"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="保管者" prop="custodian">
            <el-input v-model="form.custodian" placeholder="请输入保管者" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="检定校准单位" prop="calibrationUnit">
            <el-input v-model="form.calibrationUnit" placeholder="请输入检定校准单位" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="检定校准时间" prop="calibrationDate">
            <el-date-picker clearable v-model="form.calibrationDate" type="date" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择检定校准时间" style="width: 100%"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="有效截至日期" prop="effectiveDate">
            <el-date-picker clearable v-model="form.effectiveDate" type="date" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择有效截至日期" style="width: 100%"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-form-item label="自动生成记录0否1是" prop="recordFlag">
          <el-input v-model="form.recordFlag" placeholder="请输入自动生成记录0否1是" />
      </el-form-item>-->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button size="small" @click="cancel">取 消</el-button>
    </div>
  </jy-dialog>
</template>
<script>
import { getBsInstrument, addBsInstrument, updateBsInstrument } from "@/api/dept/bsInstrument";
import { listBsDepartment } from "@/api/dept/bsDepartment";
import { mapGetters } from "vuex";

export default {
  name: "BsInstrumentAddOrUpdate",
  computed: {
    ...mapGetters(["deptId"])
  },
  data() {
    return {
      bsDataReportList: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        instruId: [{ required: true, message: "仪器id不能为空", trigger: "blur" }],
        dpmId: [{ required: true, message: "所属科室id不能为空", trigger: "blur" }],
        instruName: [{ required: true, message: "仪器名称不能为空", trigger: "blur" }],
        instruCode: [{ required: true, message: "仪器编号不能为空", trigger: "blur" }]
      }
    };
  },
  created() {},
  mounted() {},
  methods: {
    init(id) {
      this.reset();
      this.open = true;
      this.form.instruId = id;
      this.getlistBsDepartment();
      if (id) {
        this.title = "修改试验室仪器";
        this.getInfo(id);
      } else {
        this.title = "添加试验室仪器";
      }
    },
    getlistBsDepartment() {
      listBsDepartment({ deptId: this.deptId }).then((response) => {
        this.bsDataReportList = response.rows;
      });
    },
    getInfo() {
      this.loading = true;
      getBsInstrument(this.form.instruId).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改试验室仪器";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.deptId = this.deptId;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.instruId != null) {
            updateBsInstrument(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.$emit("callBack");
                this.cancel();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addBsInstrument(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.$emit("callBack");
                this.cancel();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        instruId: undefined,
        dpmId: undefined,
        instruName: undefined,
        instruCode: undefined,
        manufacturer: undefined,
        model: undefined,
        measuringRange: undefined,
        manufacturerCode: undefined,
        manufacturerDate: undefined,
        purchaseDate: undefined,
        custodian: undefined,
        calibrationUnit: undefined,
        calibrationDate: undefined,
        effectiveDate: undefined,
        recordFlag: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    }
  }
};
</script>
<style lang="scss" scoped></style>
